<template>
  <div>
    <!-- 三级联动组件 -->
    <TypeNav/>
    <ListContainer/>
    <Recommend/>
    <Rank/>
    <Like/>
    <Floor v-for="(floor,index) in floorList" :key="floor.id" :list="floor"/>
    <Brand/>
  </div>
</template>

<script>
import ListContainer from '@/views/Home/ListContainer';
import Recommend from '@/views/Home/Recommend';
import Rank from '@/views/Home/Rank';
import Like from '@/views/Home/Like';
import Floor from '@/views/Home/Floor';
import Brand from '@/views/Home/Brand';
import { mapState } from 'vuex';
export default {
    name: "Home",
    components:{ ListContainer, Recommend, Rank, Like, Floor, Brand },
    created() {
      let code = this.getCode();
      if (code) {
        // 向服务端发送请求，并携带code，获取带有token用户信息，设置vuex中token和userInfo
        this.ssoLogin(code);
        //登录的路由组件：看路由当中是否包含query参数，有：调到query参数指定路由，没有：调到home
        let toPath = this.$route.query.redirect || "/home";
        this.$router.push(toPath);
      }else{}
    },
    mounted(){
        this.$store.dispatch('getFloorList');
        // 获取用户信息在首页展示
        this.$store.dispatch('getUserInfo');
    },
    computed:{
        ...mapState({
            floorList:state=>state.home.floorList
        })
    },
    methods: {
        getCode() {
          let arr = window.location.href.split("?");
          let code;
          if (arr.length > 1) {
            code = arr[1].split("=");
            if (code[0] == "code") {
              code = code[1].split("#")[0];
            } else {
              code = null;
            }
          }
          return code;
        },
        async ssoLogin(code){
          await this.$store.dispatch('ssoUserLogin', code);
          await this.$store.dispatch('getUserInfo');
        }
    },
    
}
</script>

<style>

</style>